<?php

require_once 'classes/LightPressPlugin.php';

class PermanentPost extends LightPressPlugin {
    
    var $constructor_args = array(
        'page_slug'=>'slug of the page to display',
        'set_permalink'=>'set the page permalink (slower as it has to fetch and build the tree of pages)',
        'use_content_filtered'=>'use the filtered content (eg with PreFormatted)');
    var $default_context = LP_CONTEXT_INDEX;
    var $description = 'Displays a page as a fixed, permanent post.';
    
    var $active = true;
    var $hooks = array('post_render');
    
    var $page_slug = '';
    var $set_permalink = false;
    var $use_content_filtered = false;
    
    function PermanentPost(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$payload) {
        
        $options =& $this->_frontend->options;
        $db =& $this->_frontend->db;
        
        $q = "select p.id as post_id, p.post_name, p.post_title, p.post_content, p.post_content_filtered, " .
            "UNIX_TIMESTAMP(p.post_date_gmt) + '" . $options['gmt_offset'] . "' as post_tstamp " .
            "from " . $this->_frontend->tables['posts'] . " p " .
            "where " .
            "p.post_status='static' and " .
            "p.post_name='" . $this->page_slug . "' and " .
            "p.post_date_gmt <= '" . $this->_frontend->where_date . "'";        
        
        if ($db->query($q) == 0)
            return $this->hide();

        $post = $db->next();
        
        $tstamp = (int)$post['post_tstamp'];
        
        if ($this->use_content_filtered && !empty($post['post_content_filtered']))
            $post['post_content'] = $post['post_content_filtered'];
        else
            $post['post_content'] = $this->_frontend->paragrapher($post['post_content']);
        
        $post['post_title'] = stripslashes($post['post_title']);
        
        $post['post_date'] = strftime($options['date_format'], $tstamp);
        $post['post_short_date'] = strftime($options['short_date_format'], $tstamp);
        $post['post_time'] = strftime($options['time_format'], $tstamp);
        $post['post_short_time'] = strftime($options['short_time_format'], $tstamp);
        $post['post_tstamp'] = $tstamp;
        
        if ($this->set_permalink)
            $post['post_permalink'] = $this->_frontend->getPagePermalink($post['post_id'], $post['post_name']);
        
        $tplvars = array();
        
        foreach ($post as $k=>$v)
            $tplvars["plugin_permanent$k"] = $v;
        
        $tpl =& $this->_frontend->tpl;
        
        $tpl->setFile('plugin_permanentpost', 'plugins/permanent_post.xml');
        $tpl->setVar($tplvars);
        $tpl->parse('PLUGIN_PERMANENTPOST', 'plugin_permanentpost');
    }
    
}

?>